package org.mathcloud.math.core.generic.operator.algebra;

import org.mathcloud.math.core.common.operator.algebra.Times;
import org.mathcloud.math.core.common.quantity.ComplexNumber;
import org.mathcloud.math.core.common.quantity.QuantityObject;
import org.mathcloud.math.core.common.quantity.RealNumber;
import org.mathcloud.math.core.common.signature.QuantityObjectSignature;
import org.mathcloud.math.core.generic.operator.GenericNaryOperator;
import org.mathcloud.math.core.generic.quantity.GenericComplexNumber;
import org.mathcloud.math.core.generic.quantity.GenericRealNumber;
import org.mathcloud.math.core.generic.signature.GenericOperatorObjectSignature;


public class GenericTimes extends GenericNaryOperator implements Times
{
	public GenericTimes()
	{
		super();
		((GenericOperatorObjectSignature)signature).setMMLContentTag("times");
	}
	
	@Override
	public QuantityObject calculate(QuantityObject operand1, QuantityObject operand2)
	{
		QuantityObjectSignature resSign = getResultSignature(operand1,operand2); 
		if (resSign!= null)
		{
			if (resSign.isRealNumberSignature())
			{
				return new GenericRealNumber(((RealNumber)operand1).getValue()*((RealNumber)operand2).getValue());
			}
			else if (resSign.isComplexNumberSignature())
			{
	      double mod, arg;
	      mod = arg = 0;

	      if (operand1.getSignature().isRealNumberSignature())
	      {
	      	mod = ((RealNumber)operand1).getValue();
	      } else if (operand1.getSignature().isComplexNumberSignature())
	      {
	        mod = ((ComplexNumber)operand1).getMod();
	        arg = ((ComplexNumber)operand1).getArg();
	      }
	      if (operand2.getSignature().isRealNumberSignature())
	      {
	        mod = mod * ((RealNumber)operand2).getValue();
	      } else if (operand2.getSignature().isComplexNumberSignature())
	      {
	        mod = mod * ((ComplexNumber)operand2).getMod();
	        arg = arg + ((ComplexNumber)operand2).getArg();
	      }
	      QuantityObject result = new GenericComplexNumber(0,0);	      
	      ((GenericComplexNumber)result).setMod(mod);
	      ((GenericComplexNumber)result).setArg(arg);
	      return result;
			}
			else return null;
		} else return null;
	}
}
